package com.xiao.service.Impl;

import com.mchange.v2.codegen.bean.ClassInfo;
import com.xiao.dao.CreateClassMapper;
import com.xiao.dao.UserMapper;
import com.xiao.pojo.*;
import com.xiao.service.UserService;
import com.xiao.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {


    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CreateClassMapper createClassMapper;

    @Override
    public List<User> findAllUser() {
        return userMapper.findAllUser();
    }


    //用户注册
    @Override
    public boolean reg(String uname, String upassword) {
        //参数不为空，且用户名未注册
        if (uname != null && upassword != null && userMapper.findUserByUname(uname) == null){
            //添加用户


            return userMapper.AddUser(uname,upassword)>0;
        }
        return false;
    }

    //用户登录
    @Override
    public String login(String uname, String upassword) {

        if (uname != null && upassword != null){
            //判断用户名密码是否一致
            User user = userMapper.findUserByUnameAndUpwd(uname, upassword);
            if (user != null){
                //把登录成功的用户信息(uid)以token形式return
                String token = JwtUtil.geneJsonWebToken(user);
                return token;
            }
        }
        return null;
    }

    //完善用户信息
    @Override
    public boolean PerfectUserInfo(long uid, String nickname, String identity, String gradename, String subjectname) {
        //参数不为空
        if (nickname != null && identity != null && gradename != null && subjectname != null){
            //根据年级名查询年级编号
            int gradeid = createClassMapper.findgradeid(gradename);
            //根据学科名查询学科编号
            int subjectid = createClassMapper.findsubjectid(subjectname);
            //组装一个user
            User user = new User(uid,nickname,identity,gradeid,subjectid);
            //完善用户信息
            return userMapper.PerfectUserInfo(user)>0;
        }

        return false;
    }

    //加入班级
    @Override
    public boolean JoinClass(long uid, long classid) {
        //判断用户是否存在  班级是否存在
        if (userMapper.findUserByUid(uid) != null && userMapper.findClassByClassid(classid) != null){
            //判断是否已加入该班级
            if (userMapper.FindClassrelationByUidAndCid(uid,classid) ==null){
                //加入班级关联表
                String power = null;
                long uid1 = userMapper.findClassByClassid(classid).getUid();
                //判断该用户是不是班级的创建者 如果是 power 就设置为 最高管理员
                if (uid == uid1){
                    power = "最高管理员";
                    Classrelation classrelation = new Classrelation(uid,classid,power);
                    return userMapper.AddClassrelation(classrelation)>0;
                }else{
                    //如果该用户是教师 power 就设置为 任课教师
                    if (userMapper.FindIdentityByUid(uid).equals("教师")){
                        power = "任课教师";
                        Classrelation classrelation2 = new Classrelation(uid,classid,power);
                        return userMapper.AddClassrelation(classrelation2)>0;
                    }else{
                        //如果该用户是学生 power 就设置为 普通成员
                        if (userMapper.FindIdentityByUid(uid).equals("学生")){
                            power = "普通成员";
                            Classrelation classrelation3 = new Classrelation(uid,classid,power);
                            return userMapper.AddClassrelation(classrelation3)>0;
                        }

                    }
                }

            }
        }
        return false;
    }

    //根据uid查询身份
    @Override
    public String FindIdentity(long uid) {
        return userMapper.FindIdentityByUid(uid);
    }

    //获得所有年级类型
    @Override
    public List<Gradetype> FindAllGradetype() {
        return createClassMapper.findAllGradetype();
    }

    //根据uid获得该用户创建的所有班级信息
    @Override
    public List<Classinfo> FindClassInfoByuid(long uid) {
        return userMapper.findClassinfoByUid(uid);
    }

    //获得所有学科信息
    @Override
    public List<Subject> FindSubjectAll() {
        return userMapper.FindAllSubject();
    }
}
